package com.myapp.ruyixing.ui.shopmanager;

import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.model.OrderInfo;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class OrderDetailActvity extends BaseActivity implements OnClickListener {

	private String mOrderId;
	private OrderInfo mInfo;

	private TextView mNameView;
	private TextView mOrderStateView;
	private TextView mTimeView;
	private TextView mSexView;
	private TextView mPhoneView;
	private TextView mOrderTimeView;
	private TextView mOrderTypeView;
	private TextView mOrderPeopleView;
	private TextView mMessageView;
	private TextView mConsumptionView;
	private TextView mRemarkView;
	private TextView mPointView;
	private TitleBar mTitleBar;
	private View mLoadingView;
	private View mBody;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				OrderInfo info = (OrderInfo) o;
				onLoadingDataFinished(true, info);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String shopbduss, String orderid) {
		showLoading();
		RYHttpClient.getOrderInfo(this, mHttpResponseHandler, orderid,
				shopbduss);
	}

	private void onLoadingDataFinished(boolean succeed, OrderInfo info) {
		hideLoading();
		if (succeed && info != null) {
			mInfo = info;
			loadViews(info);
		} else {
			Toast.makeText(this, "加载失败" + (info == null ? "" : info.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	private RYHttpResponseHandler mDeleteHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model resul = (Model) o;
				onDelteFinished(true, resul);
			} catch (Exception e) {
				onDelteFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onDelteFinished(false, null);
		}
	};

	private void onDeleteStarted(String shopbduss, String shopid, String orderid) {
		showLoading();
		RYHttpClient.delOrder(this, mDeleteHttpResponseHandler, shopbduss,
				shopid, orderid);
	}

	private void onDelteFinished(boolean succeed, Model result) {
		hideLoading();
		if (succeed && result != null && result.errno == 0) {
			Toast.makeText(this, result.msg, Toast.LENGTH_SHORT).show();
			finish();
		} else {
			Toast.makeText(this, "删除失败:" + (result == null ? "" : result.msg),
					Toast.LENGTH_SHORT).show();
		}
	}

	private void loadViews(OrderInfo info) {
		mBody.setVisibility(View.VISIBLE);
		mConsumptionView.setText("消费金额：" + info.price);
		mMessageView.setText("客户留言：" + info.content);
		mNameView.setText("姓名：" + info.name);
		mOrderPeopleView.setText("预定人数：" + info.persons);
		mOrderStateView.setText(info.status);
		mOrderTimeView.setText("预定时间：" + info.reversetime);
		mOrderTypeView.setText("预定类型：" + info.type);
		mPhoneView.setText("电话：" + info.phone);
		mPointView.setText("赠送积分：" + info.awardpoint);
		mRemarkView.setText("商家备注：" + info.remark);
		mSexView.setText("性别：" + info.sex);
		mTimeView.setText(info.datetime);

		addFontColor();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_order_detail);

		initView();

		mOrderId = getIntent().getStringExtra("orderid");
	}

	@Override
	protected void onResume() {
		onLoadingDataStarted(LoginHelper.getInstance().getShopBduss(), mOrderId);
		super.onResume();
	}

	private void initView() {
		mBody = findViewById(R.id.layout_content);
		findViewById(R.id.update_status).setOnClickListener(this);
		findViewById(R.id.notify_user).setOnClickListener(this);
		findViewById(R.id.delete).setOnClickListener(this);

		mNameView = (TextView) findViewById(R.id.name);
		mOrderStateView = (TextView) findViewById(R.id.order_state);
		mTimeView = (TextView) findViewById(R.id.time);
		mSexView = (TextView) findViewById(R.id.sex);
		mPhoneView = (TextView) findViewById(R.id.phone);
		mOrderTimeView = (TextView) findViewById(R.id.order_time);
		mOrderTypeView = (TextView) findViewById(R.id.order_type);
		mOrderPeopleView = (TextView) findViewById(R.id.order_people);
		mMessageView = (TextView) findViewById(R.id.message);
		mConsumptionView = (TextView) findViewById(R.id.consumption);
		mRemarkView = (TextView) findViewById(R.id.remark);
		mPointView = (TextView) findViewById(R.id.point);

		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);
		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	private void addFontColor() {
		String text = mOrderPeopleView.getText().toString();
		Spannable wordtoSpan = new SpannableString(text);

		wordtoSpan.setSpan(
				new ForegroundColorSpan(getResources().getColor(
						R.color.text_order_count)), 5, text.length(),
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		mOrderPeopleView.setText(wordtoSpan);

		text = mOrderTypeView.getText().toString();
		wordtoSpan = new SpannableString(text);
		wordtoSpan.setSpan(
				new ForegroundColorSpan(getResources().getColor(
						R.color.text_order_count)), 5, text.length(),
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		mOrderTypeView.setText(wordtoSpan);
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_status:
			startActivity(new Intent(this, OrderUpdateStatusActivity.class)
					.putExtra("status", mInfo.status.trim())
					.putExtra("remark", mInfo.remark.trim())
					.putExtra("orderid", mOrderId)
					.putExtra("price", mInfo.price));
			break;
		case R.id.notify_user:
			startActivity(new Intent(this, OrderNotifyUserActivity.class)
					.putExtra("name", mInfo.name.trim())
					.putExtra("phone", mInfo.phone.trim())
					.putExtra("orderid", mOrderId));
			break;
		case R.id.delete:
			onDeleteStarted(LoginHelper.getInstance().getShopBduss(),
					LoginHelper.getInstance().getShopId(), mOrderId);
			break;

		default:
			break;
		}
	}
}
